
/*
 * Initialization
 */

YAHOO.util.Event.addListener(window, 'load', YAHOO.dasBrowser.genome.request.startGenomeDasRequest);




/*
 * External ource stylesheet Callback
 */
var externalSourceStylesheetCallback = {
	timeout: 10000,
    success:function(o){
		var sourceStylesheetResults = YAHOO.dasBrowser.genome.karyotypeStructure.karyotype.stylesheet.parse(o.responseXML);
		patientSource(sessvars.karyoDas.conf.decipher.patient.source.annotations, sessvars.karyoDas.conf.decipher.patient.source.name, sourceStylesheetResults);
    },
    failure:function(o){
		patientSource(sessvars.karyoDas.conf.decipher.patient.source.annotations, sessvars.karyoDas.conf.decipher.patient.source.name, null);
    }
};



/*
 * Add external sources 
 */
patientSource = function(externalAnnotations, sourceName, sourceStylesheetResults){
	
var sourceArrayNumber = YAHOO.dasBrowser.genome.annotations.sources.length;
if(YAHOO.dasBrowser.genome.annotations.sources[sourceArrayNumber-1].name != sourceName){	

	Dresult = {
	chromosome1: {bv: {features: new Array(), segment: null}, cv: {features: new Array(), segment: null}, kv: {features: new Array(), segment: null}, zv: {features: new Array(), segment: null}, higherDefinitionView: "Kv"},
	chromosome2: {bv: {features: new Array(), segment: null}, cv: {features: new Array(), segment: null}, kv: {features: new Array(), segment: null}, zv: {features: new Array(), segment: null}, higherDefinitionView: "Kv"},
	chromosome3: {bv: {features: new Array(), segment: null}, cv: {features: new Array(), segment: null}, kv: {features: new Array(), segment: null}, zv: {features: new Array(), segment: null}, higherDefinitionView: "Kv"},
	chromosome4: {bv: {features: new Array(), segment: null}, cv: {features: new Array(), segment: null}, kv: {features: new Array(), segment: null}, zv: {features: new Array(), segment: null}, higherDefinitionView: "Kv"},
	chromosome5: {bv: {features: new Array(), segment: null}, cv: {features: new Array(), segment: null}, kv: {features: new Array(), segment: null}, zv: {features: new Array(), segment: null}, higherDefinitionView: "Kv"},
	chromosome6: {bv: {features: new Array(), segment: null}, cv: {features: new Array(), segment: null}, kv: {features: new Array(), segment: null}, zv: {features: new Array(), segment: null}, higherDefinitionView: "Kv"},
	chromosome7: {bv: {features: new Array(), segment: null}, cv: {features: new Array(), segment: null}, kv: {features: new Array(), segment: null}, zv: {features: new Array(), segment: null}, higherDefinitionView: "Kv"},
	chromosome8: {bv: {features: new Array(), segment: null}, cv: {features: new Array(), segment: null}, kv: {features: new Array(), segment: null}, zv: {features: new Array(), segment: null}, higherDefinitionView: "Kv"},
	chromosome9: {bv: {features: new Array(), segment: null}, cv: {features: new Array(), segment: null}, kv: {features: new Array(), segment: null}, zv: {features: new Array(), segment: null}, higherDefinitionView: "Kv"},
	chromosome10: {bv: {features: new Array(), segment: null}, cv: {features: new Array(), segment: null}, kv: {features: new Array(), segment: null}, zv: {features: new Array(), segment: null}, higherDefinitionView: "Kv"},
	chromosome11: {bv: {features: new Array(), segment: null}, cv: {features: new Array(), segment: null}, kv: {features: new Array(), segment: null}, zv: {features: new Array(), segment: null}, higherDefinitionView: "Kv"},
	chromosome12: {bv: {features: new Array(), segment: null}, cv: {features: new Array(), segment: null}, kv: {features: new Array(), segment: null}, zv: {features: new Array(), segment: null}, higherDefinitionView: "Kv"},
	chromosome13: {bv: {features: new Array(), segment: null}, cv: {features: new Array(), segment: null}, kv: {features: new Array(), segment: null}, zv: {features: new Array(), segment: null}, higherDefinitionView: "Kv"},
	chromosome14: {bv: {features: new Array(), segment: null}, cv: {features: new Array(), segment: null}, kv: {features: new Array(), segment: null}, zv: {features: new Array(), segment: null}, higherDefinitionView: "Kv"},
	chromosome15: {bv: {features: new Array(), segment: null}, cv: {features: new Array(), segment: null}, kv: {features: new Array(), segment: null}, zv: {features: new Array(), segment: null}, higherDefinitionView: "Kv"},
	chromosome16: {bv: {features: new Array(), segment: null}, cv: {features: new Array(), segment: null}, kv: {features: new Array(), segment: null}, zv: {features: new Array(), segment: null}, higherDefinitionView: "Kv"},
	chromosome17: {bv: {features: new Array(), segment: null}, cv: {features: new Array(), segment: null}, kv: {features: new Array(), segment: null}, zv: {features: new Array(), segment: null}, higherDefinitionView: "Kv"},
	chromosome18: {bv: {features: new Array(), segment: null}, cv: {features: new Array(), segment: null}, kv: {features: new Array(), segment: null}, zv: {features: new Array(), segment: null}, higherDefinitionView: "Kv"},
	chromosome19: {bv: {features: new Array(), segment: null}, cv: {features: new Array(), segment: null}, kv: {features: new Array(), segment: null}, zv: {features: new Array(), segment: null}, higherDefinitionView: "Kv"},
	chromosome20: {bv: {features: new Array(), segment: null}, cv: {features: new Array(), segment: null}, kv: {features: new Array(), segment: null}, zv: {features: new Array(), segment: null}, higherDefinitionView: "Kv"},
	chromosome21: {bv: {features: new Array(), segment: null}, cv: {features: new Array(), segment: null}, kv: {features: new Array(), segment: null}, zv: {features: new Array(), segment: null}, higherDefinitionView: "Kv"},
	chromosome22: {bv: {features: new Array(), segment: null}, cv: {features: new Array(), segment: null}, kv: {features: new Array(), segment: null}, zv: {features: new Array(), segment: null}, higherDefinitionView: "Kv"},
	chromosomeX: {bv: {features: new Array(), segment: null}, cv: {features: new Array(), segment: null}, kv: {features: new Array(), segment: null}, zv: {features: new Array(), segment: null}, higherDefinitionView: "Kv"},
	chromosomeY: {bv: {features: new Array(), segment: null}, cv: {features: new Array(), segment: null}, kv: {features: new Array(), segment: null}, zv: {features: new Array(), segment: null}, higherDefinitionView: "Kv"}
	}


	Dsource = {
	align: "bottom",
	arrayPosition: 99,
	color: "0000FF",
	cvLoaded: false,
	description: sourceName,
	highetScore: 0,
	name: sourceName,
	label: ["ENSEMBL"],
	loadCompleted: false,
	lowestScore: 0,
	id: "External source",
	ntLimit: null,
	panel: "karyotype",
	position: 1,
	representation: "track",
	result: Dresult,
	segmentCount: 0,
	stylesheet: true,
	stylesheetData: new Object(),
	types: [],
	url: "none"
	}
	
	/*
	eA.id = "Variation1";
	eA.start = "1";
	eA.end = "167209";
	eA.typeName = "Patient annotation";
	eA.chromosomeId = "chromosome1";
	eA.setLinkText = "url text";
	eA.setLinkUrl = "url";
	*/
	
	
	for (var j = 0; j < externalAnnotations.length; j++) {
		var annotationFeature = new myAnnotationFeature();
		annotationFeature.setStart(externalAnnotations[j].start);
		annotationFeature.setEnd(externalAnnotations[j].end);
		annotationFeature.setId(externalAnnotations[j].id);
		annotationFeature.setTypeId(externalAnnotations[j].typeId);
		annotationFeature.setTypeCategory(externalAnnotations[j].typeName);
		annotationFeature.setLinkText(externalAnnotations[j].linkText);
		annotationFeature.setLinkUrl(externalAnnotations[j].linkUrl);
		annotationFeature.sourceName = sourceName;
		annotationFeature.chromosomeId = externalAnnotations[j].chromosomeId;
		
		var annotationSegment = Object();
		if(Dresult[externalAnnotations[j].chromosomeId].kv.segment != null){
			annotationSegment = Dresult[externalAnnotations[j].chromosomeId].kv.segment;
		} else {
			annotationSegment = new myAnnotationSegment();
		}
		annotationSegment.setPositional();
		
		Dresult[externalAnnotations[j].chromosomeId].kv.features.push(annotationFeature);
		Dresult[externalAnnotations[j].chromosomeId].kv.segment = annotationSegment;
	}
	
	
	//var annotationSegment = new myAnnotationSegment();
	//annotationSegment.setPositional(); // +1
	//annotationSegment.setVersion("1.0");
	//annotationSegment.setStop("2492250621");
	
	
	YAHOO.dasBrowser.genome.annotations.sources[sourceArrayNumber] = Dsource;
	YAHOO.dasBrowser.genome.annotations.sources[sourceArrayNumber].arrayPosition = sourceArrayNumber;
	if(sourceStylesheetResults == null){
		YAHOO.dasBrowser.genome.annotations.sources[sourceArrayNumber].stylesheet = false;
	} else {
		YAHOO.dasBrowser.genome.annotations.sources[sourceArrayNumber].stylesheetData;
		YAHOO.dasBrowser.genome.annotations.sources[sourceArrayNumber].stylesheetData.types = new Object();
		YAHOO.dasBrowser.genome.annotations.sources[sourceArrayNumber].stylesheetData.types = sourceStylesheetResults.allKaryotypeStylesheetTypes;
	}




						var selectedEntryPoints = YAHOO.dasBrowser.genome.karyotypeStructure.entryPoints.selectedEntryPoints;
						var features = new Object();
						var segment = new Object();
	
						//CHROMOSOME
						var selectedChromosome = YAHOO.dasBrowser.genome.karyotypeStructure.karyotype.chromosomeConf.selectedChromosome;
						//features = annotationSource.result[selectedChromosome].kv.features;
						//segment = annotationSource.result[selectedChromosome].kv.segment;
						features = Dsource.result[selectedChromosome].kv.features;
						segment = Dsource.result[selectedChromosome].kv.segment;
						
						var positionalFeatures = 0;
						if(segment != null){
							YAHOO.dasBrowser.genome.karyotypeStructure.karyotype.drawAnnotations(features, segment, null, selectedChromosome, "Cv", sourceArrayNumber);								
							positionalFeatures = segment.positional;
						}
							//console.log("j"+positionalFeatures);
						YAHOO.dasBrowser.genome.annotations.simpleView.drawLegend("Cv", selectedChromosome, sourceArrayNumber, positionalFeatures);
						//YAHOO.dasBrowser.genome.request.drawing.annotations.completed++;
							
						
						//KARYOTYPE
						for (var i = 0; i < selectedEntryPoints.ids.length; i++) {
							//setTimeout('YAHOO.dasBrowser.genome.request.ajaxRequestControl(\'' + "annotation" + '\',\'' + "success" + '\',' + sourceArrayNumber + ',\'' + selectedEntryPoints.ids[i] + '\',\'' + "Kv" + '\')', 0);
							if(Dsource.result[selectedEntryPoints.ids[i]]){
								features = Dsource.result[selectedEntryPoints.ids[i]].kv.features;
								segment = Dsource.result[selectedEntryPoints.ids[i]].kv.segment;
								var positionalFeatures = 0;
								if(segment != null){
									YAHOO.dasBrowser.genome.karyotypeStructure.karyotype.drawAnnotations(features, segment, null, selectedEntryPoints.ids[i], "Kv", sourceArrayNumber);
									positionalFeatures = segment.positional;
								}
								//console.log("j"+positionalFeatures);
								YAHOO.dasBrowser.genome.annotations.simpleView.drawLegend("Kv", selectedEntryPoints.ids[i], sourceArrayNumber, positionalFeatures);
							}
							//YAHOO.dasBrowser.genome.request.drawing.annotations.completed++;
						}
							
	}
}